﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    /*  This class is used to access the CompanyAccount.
     *  We can say that the CompanyAccount is the master account.
     *  Employer classes are mini passports used to access the CompanyAccount.  */
    public class DBEmployerPassport
    {
        

        //add
        public static bool AddEmployerPassport(EmployerPassport ep, CompanyAccount ca)
        {
            try
            {
                ep.EPassportID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(ca);
                    ca.EmployerPassports.Add(ep);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve
        public static EmployerPassport RetrieveEmployerPassport(string passportID)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                    return
                    (from e in db.EmployerPassports
                     where e.EPassportID == passportID
                     select e).SingleOrDefault();
            }
        }

        public static EmployerPassport RetrieveEmployerPassportByUsername(string username)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from e in db.EmployerPassports
                    where e.Username == username
                    select e).SingleOrDefault();
            }
        }

        public static EmployerPassport RetrieveEmployerPassportByEmail(string email)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from ep in db.EmployerPassports
                     where ep.Email == email
                     select ep).SingleOrDefault();
            }
        }

        public static List<EmployerPassport> RetrieveEmployerPassportsByCompanyAccount(CompanyAccount ca)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from ep in db.EmployerPassports
                     .Where(ep => ep.CompanyAccount == ca)
                     select ep).ToList<EmployerPassport>();
            }
        }

        //update
        public static bool UpdateEmployerPassport(EmployerPassport ep)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    EmployerPassport passportToUpdate = RetrieveEmployerPassport(ep.EPassportID);
                    db.Attach(passportToUpdate);
                    passportToUpdate.FirstName = ep.FirstName;
                    passportToUpdate.LastName = ep.LastName;
                    passportToUpdate.SecurityQuestion = ep.SecurityQuestion;
                    passportToUpdate.SecurityAnswer = ep.SecurityAnswer;
                    passportToUpdate.Password = ep.Password;
                    passportToUpdate.Username = passportToUpdate.Username;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        //delete
        public static bool DeleteEmployerPassport(EmployerPassport ep)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    EmployerPassport passportToDelete = RetrieveEmployerPassport(ep.EPassportID);
                    db.Attach(passportToDelete);
                    db.DeleteObject(passportToDelete);

                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool DeleteEmployerPassportById(string id)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    EmployerPassport passportToDelete = RetrieveEmployerPassport(id);
                    db.Attach(passportToDelete);
                    db.DeleteObject(passportToDelete);

                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}